using System.Threading;
using AZROLESLib;
using AzManContrib.MVC.Decorators;

namespace AzManContrib.MVC.Initialization
{
    public class Asynchronously : IAzManInitializationType
    {
        
        public void Init(InitializationContext initializationContext)
        {
            ThreadPool.QueueUserWorkItem(BaseInit, initializationContext);
        }

        private void BaseInit(object initializationContext)
        {
            InitializationContext ct = (InitializationContext) initializationContext;
            
            // assigning a mocked store for initialization usage.
            AzMan.AzManStore = new WrappingAzManAuthorizationStore();
            
            // initializing the real AzMan store.
            IAzAuthorizationStore realStore = new AzAuthorizationStoreClass();
            realStore.Initialize(0, ct.ConnectionString, null);
            
            // switch to the real AzMan store
            AzMan.AzManStore = new AzManAuthorizationStoreDecorator(realStore);

            AzMan.AzManApplication = AzMan.AzManStore.OpenApplication(ct.ApplicationName);
            AzMan.Initialized = true;
        }
    }
}